var mask = null;
var login_user = null;

$(function() { // 初始化内容
	mask = mui.createMask(); //遮罩层
	datetime_init();
	console.log("aaaaa---------------")
	var cld = new Calendar({
		el: '#box',
		value: '', // 默认为new Date();
		callback: function(obj) {

		}
	})
	login_user = JSON.parse(localStorage.getItem('loginUser'));
	console.log(login_user)
	var donetimeStart = new Date().format("yyyy-MM-01 00:00:00");
	var main_width = jQuery("#app").width();
	jQuery("#calender").width(main_width);

	setTimeout(function() {
		console.log(donetimeStart)
		donetimeStart = startCurrentMonth();
		donetimeEnd = endCurrentMonth();
		loadCurriculumList(donetimeStart, donetimeEnd);

	}, 500)
	setTimeout(function() {
		console.log(donetimeStart)
		donetimeStart = srartCurentTime();
		donetimeEnd = endtCurentTime();
		loadCurriculum(donetimeStart, donetimeEnd);

	}, 500)
});







//初始化课程日历
function loadCurriculum(donetimeStart, donetimeEnd) {

	console.log("donetimeStart====" + donetimeStart);
	console.log("donetimeEnd====" + donetimeEnd);
	var data = {};
	data["coursedatestart"] = donetimeStart
	data['coursedateend'] = donetimeEnd;
	data['rows'] = 1000;
	data['page'] = 1;
	data['sort'] = "id";
	data['teacherid'] = login_user.id;
	console.log("教师本月课程上传====" + JSON.stringify(data));
	jQuery.ajax({
		headers: {
			'Authorization': login_user.token
		},
		type: "post",
		url: serverip + "jw/jsrc",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层
		},
		success: function(res) {
			mask.close(); //显示遮罩层
			console.log("教师本月课程====" + JSON.stringify(res));

			loadApp(res.courselist)

		},
		error: function(e) {

			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));

		}
	});
}

//加载课程列表
function loadCurriculumList(donetimeStart, donetimeEnd) {

	console.log("donetimeStart====" + donetimeStart);
	console.log("donetimeEnd====" + donetimeEnd);

	var data = {};
	data['teacherid'] = login_user.id;
	data['coursedatestart'] = donetimeStart;
	data["coursedateend"] = donetimeEnd;
	data['rows'] = 20;
	data['page'] = 1;
	data["sort"] = "id";
	console.log("课程列表上传参数====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "jw/jsrc",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
			app.todos = [];
		},
		success: function(res) {

			mask.close(); //显示遮罩层 	    	
			console.log("获取课程列表=====" + JSON.stringify(res));
			loadCalendar(res.courselist)



		},

		error: function(e) {

			mask.close(); //显示遮罩层
			mui.alert('系统错误');
			console.log(JSON.stringify(e));
		}
	});
}


function loadCalendar(res) {
	console.log("res====" + JSON.stringify(res))
	var canChooses = document.querySelectorAll(".canChoose");

	var arr = []
	for (var i = 0; i < res.length; i++) {
		var opentime = null;

		if (res[i].coursetypes == "普通课程") {
			var data = res[i].coursedate;
			opentime = data.split(' ')[0].split('-')[2];
		} else if(res[i].coursetypes == "大课"){
			var data = res[i].startcoursetime;
			opentime = data.split(' ')[0].split('-')[2];
		}
		console.log("opentime======" + opentime)
		arr.push(parseInt(opentime))

	}
	arr = unique(arr);
	console.log(arr)
	for (var i = 0; i < arr.length; i++) {
		var num = arr[i]
		canChooses[num - 1].classList.add("blue");

	}


}
//数组去重
function unique(arr) {
	if (!Array.isArray(arr)) {
		console.log('type error!')
		return
	}
	var array = [];
	for (var i = 0; i < arr.length; i++) {
		if (array.indexOf(arr[i]) === -1) {
			array.push(arr[i])
		}
	}
	return array;
}

var app = new Vue({
	el: '#app',
	data: {
		show: true,
		todos: []
	},
	methods: {
		selected: function(todo) {
			
			console.log(JSON.stringify(todo))


			window.localStorage.setItem("todo", JSON.stringify(todo))
			window.location.href = "course_form.html"

		}
	}
});



function loadApp(data) {
	var arry = new Array();
	for (var i = 0; i < data.length; i++) {
		var d = data[i];
		var o = new Object();
		o.id = d.id;
		o.coursedate = d.coursedate;
		o.coursename = d.coursename + "第" + d.coursenum + "期";
		o.coursetype = d.coursetypes;
		o.schoolname = d.schoolname;
		o.classlocation = d.classlocation;
		if(d.coursetypes == "普通课程"){
			o.startcoursetime = d.startcoursetime;
			o.endcoursetime = d.endcoursetime;
		}else if(d.coursetypes == "大课"){
			o.startcoursetime = d.startcoursetime;
			o.endcoursetime = d.endcoursetime;
		}
		o.upsigndate = d.upsigndate;
		o.teachername = d.teachername;
		if (o.upsigndate != null) {
			o.isSign = "已签到"
		}
		arry.push(o);
	}
	console.log(JSON.stringify(arry));

	app.todos = arry;


}


//获取当前时间
function srartCurentTime() {
	var now = new Date();

	var year = now.getFullYear(); //年
	var month = now.getMonth() + 1; //月
	var day = now.getDate(); //日

	var hh = "0" //时
	var mm = "0"; //分

	var clock = year + "-";

	if (month < 10)
		clock += "0";

	clock += month + "-";

	if (day < 10)
		clock += "0";

	clock += day + " ";

	
	clock += "0";

	clock += hh + ":";
	if (mm < 10) clock += '0';
	clock += mm;
	clock += ":00"
	return (clock);
}

//获取当前时间
function endtCurentTime() {
	var now = new Date();

	var year = now.getFullYear(); //年
	var month = now.getMonth() + 1; //月
	var day = now.getDate(); //日

	var hh = "23"; //时
	var mm = "59"; //分

	var clock = year + "-";

	if (month < 10)
		clock += "0";

	clock += month + "-";

	if (day < 10)
		clock += "0";

	clock += day + " ";

	if (hh < 10)
		clock += "0";

	clock += hh + ":";
	if (mm < 10) clock += '0';
	clock += mm;
	clock += ":00"
	return (clock);
}
